package com.main.ok.common;

import java.io.IOException;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class RunSqlTool {
	private static JdbcTemplate jdbcTemplate;

	public static void main(String[] args) {
		// List rows =
		// jdbcTemplate.queryForList("select * from msg_his limit 100");
		// Iterator it = rows.iterator();
		// while (it.hasNext()) {
		// Map map = (Map) it.next();
		// System.out.println(map.get("id") + "----" + map.get("content"));
		// }
	}

	/**
	 * retrun jdbc template
	 * 
	 * @return
	 */
	public static JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	/**
	 * run sql
	 * 
	 * @param sql
	 */
	public static void executeSql(String sql) {
		jdbcTemplate.execute(sql);
		System.out.println(String.format("sql:[%s] has been executed===", sql));
	}

	static {
		Resource resource = new ClassPathResource("/push-dal-config.properties");
		try {
			Properties p = PropertiesLoaderUtils.loadProperties(resource);
			String driverClassName = p.getProperty("greenline.push.mysql.driver");
			String url = p.getProperty("greenline.push.mysql.url");
			String username = p.getProperty("greenline.push.mysql.username");
			String password = p.getProperty("greenline.push.mysql.password");

			DriverManagerDataSource ds = new DriverManagerDataSource();
			ds.setUrl(url);
			ds.setDriverClassName(driverClassName);
			ds.setUsername(username);
			ds.setPassword(password);

			jdbcTemplate = new JdbcTemplate(ds);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
